import random
from core.basehandler import BaseHandler
from core.room import Room
from core.action import Action
from core.support import get_game

class Battle(Room):
    
    def __init__(self, room_id):
        Room.__init__(self, room_id)    # do basic initialization for every room
        
        self.add_action(ShowResult) 
        
    # return description of room.
    def get_description(self):
        return '''
        You are now going to battle!
         '''
    # return image of room
    def get_image(self):
        return 'battle.jpg'

class BattleHandler(BaseHandler):
    
    # return class to use to create a new instance of this room
    def get_room_class(self):
        return Battle
    
    def get_template(self):
        return 'battle.html'

class ShowResult(Action):
    def __init__(self):
        Action.__init__(self, "ShowResult")    # do basic initialization for every action
    
    def get_description(self):
        return "Show the Result"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        winner = self.do_battle()
        if winner == "Attack":
            return 'lannisterwins'
        else:
            return 'lannisterloses'
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "get"

    def attack(self):
        game = get_game()
        archer = int(game.archer)
        cavalry = int(game.cavalry)
        infantry = int(game.infantry)
        return archer*1 + cavalry*3 + infantry*2
   
    def do_battle(self):
        attackDamg = 0
        defendDamg = 0
        attackHP = self.attack()
        defendHP = 1250
        
        while attackHP > 0 and defendHP>0:
            attackDamg=random.randint(0, 100)
            defendHP-=attackDamg
            defendDamg=random.randint(0, 100)
            attackHP-=defendDamg
        
        if attackHP > defendHP:
            return "Attack"
        else:
            return "Defend"
    
